package com.ruoyi.ai.service.impl;

import com.ruoyi.ai.service.VectorService;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author coach tam
 * @email 327395128@qq.com
 * @values 坚持灵活 灵活坚持
 * @since 2025/3/24
 */
@Service
public class VectorServiceImpl implements VectorService {
    @Autowired
    @Qualifier("milvusVectorStore")
    private VectorStore vectorStore;
    @Override
    public List<String> searchQuestion(String question) {
//        SearchRequest searchRequest = SearchRequest.builder().query(question).topK(1).similarityThreshold(0.7).build();
        SearchRequest searchRequest = SearchRequest.builder().query(question).topK(1).similarityThreshold(0.8).build();
        List<Document> documents = vectorStore.similaritySearch(searchRequest);
        return  documents.stream().map(doc-> doc.getMetadata().getOrDefault("content", "").toString()).toList();
    }

    @Override
    public List<String> searchQuestion2(String userInput) {
        // 可以根据实际情况调整topK和similarityThreshold的值
        // topK 增大可以获取更多可能的结果，再从中筛选；similarityThreshold 增大可以提高匹配的严格程度
        SearchRequest searchRequest = SearchRequest.builder()
                .query(userInput)
                .topK(1) // 增大topK以获取更多可能的结果
                .similarityThreshold(0.8) // 提高相似度阈值以提高匹配的严格程度
                .build();
                List<Document> documents = vectorStore.similaritySearch(searchRequest);
                return  documents.stream().map(doc-> doc.getMetadata().getOrDefault("content", "").toString()).toList();
    }

}
